﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace ProjectBase.Mvc
{
    public class AnchorOrFile : DetailLinkControl<byte[], AnchorOrFile>
    {
        #region members
        protected const string Target = "newWindow";
        #endregion members

        #region constructor
        public AnchorOrFile() { }
        public AnchorOrFile(bool? asReadonly = null) : base(asReadonly) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, byte[] value)
        {
            base.OnSourcePropertyChanged(propertyName, value);

            if (value.Is())
            {
                SetDetailController(null);
                SetDetailAction(propertyName);
                SetText(propertyName);
                SetFileExists(value.Is());
            }
        }
        #endregion OnSourceProperty

        #region Render
        protected override void ReadControls()
        {
            Controls.Add
                (
                    new Div(GetAttribute(Attr.Class))
                    {
                        new Span
                        {
                            new Anchor()
                                .SetTitle(Text.Length > MaxLength ? Text : null)
                                .SetTitle(Title)
                                .SetHref(HasDetailLink && FileExists ? DetailUrl : null)
                                .SetTarget(AnchorTarget ?? Target)
                                .SetCssClassName(HasDetailLink && FileExists ? null : "none noPointer")
                                .AddControls
                                (
                                    new Literal(Text.ToLength(MaxLength, true))
                                )
                        }
                    }
                );
        }

        protected override void WriteControls()
        {
            Controls.Add
            (
                new PlaceHolder
                {
                    new Input(InputType.File)
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .SetName(InputName),
                    new Input(InputType.Hidden)
                        .SetName(InputName + "FileName"), // to bind string property ending with FileName (see SmartBinder)
                }
            );
        }
        #endregion Render

        #region Set()
        /// <summary>
        ///     Dynamically sets the property AnchorTarget. Default value is "newWindow"
        /// </summary>
        /// <param name = "anchorTarget">not null nor empty action name</param>
        /// <returns>this</returns>
        public virtual AnchorOrFile SetAnchorTarget(string anchorTarget)
        {
            if (anchorTarget.IsNotEmpty())
            {
                AnchorTarget = anchorTarget;
            }
            return this;
        }

        /// <summary>
        ///     Is there any file to be accessed via detail link?
        /// </summary>
        /// <param name = "fileExists"></param>
        /// <returns>this</returns>
        public virtual AnchorOrFile SetFileExists(bool fileExists)
        {
            FileExists = fileExists;
            return this;
        }
        #endregion public Set()

        #region properties
        /// <summary>
        ///     Allows to adjust which window will be used for FileCoreResult target
        ///     (default is const string "newWindow")
        /// </summary>
        public virtual string AnchorTarget { get; set; }

        /// <summary>
        ///     Is there any file to be accessed via detail link?
        /// </summary>
        public virtual bool FileExists { get; set; }

        public override string DetailController
        {
            get
            {
                if (base.DetailController.IsEmpty())
                {
                    var model = Model as IViewModel;
                    if (model.Is())
                    {
                        base.DetailController = model.MasterModel.ControllerName;
                    }
                }
                return base.DetailController;
            }
            set { base.DetailController = value; }
        }

        public override string DetailEntityID
        {
            get
            {
                if (base.DetailEntityID.IsEmpty())
                {
                    var model = Model as IEntityModel<IPersistentObject, IFilter>;
                    if (model.Is()
                        && model.Item.Is())
                    {
                        base.DetailEntityID = model.Item.IDToDisplay;
                    }
                }
                return base.DetailEntityID;
            }
            set { base.DetailEntityID = value; }
        }
        #endregion properties
    }
}